<?php
include_once LIB_PATH . 'cls.addon.php';

class TinyEditor extends Addon
{

  public function tinymce()
  {
    echo '<script src="/editor/tinymce/tinymce.min.js"></script>';
    echo <<<TINYMCE
      <script>
      tinymce.init({
        selector: '.editor',
        language: 'zh_CN',
        menubar: false,
        forced_root_block: 'div',
        height: '50vh',
        plugins: 'paste print preview searchreplace autolink fullscreen image link media code codesample table charmap hr pagebreak nonbreaking anchor advlist lists textpattern help emoticons autosave bdmap indent2em lineheight axupimgs',
        toolbar: 'code undo redo restoredraft | indent2em alignleft aligncenter alignright alignjustify | table image media charmap emoticons hr bdmap axupimgs | forecolor backcolor bold italic underline strikethrough link | bullist numlist blockquote subscript superscript removeformat | formatselect fontselect fontsizeselect | preview print fullscreen',
        fixed_toolbar_container: '#doc-app .toolbar',
        custom_ui_selector: 'body',
        placeholder: '',
TINYMCE;
    echo 'toolbar_mode: "' . (!is_mobile() ? 'wrap' : 'scrolling') . '",';
    echo <<<TINYMCE
        toolbar_sticky: true,
        autosave_ask_before_unload: true,
        fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
        font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;知乎配置=BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif;小米配置=Helvetica Neue,Helvetica,Arial,Microsoft Yahei,Hiragino Sans GB,Heiti SC,WenQuanYi Micro Hei,sans-serif',
        images_upload_url: '?s=admin&m=upload&c=image',
        images_upload_handler: function (blobInfo, succFun, failFun) {
          var xhr, formData;
          var file = blobInfo.blob();//转化为易于理解的file对象
          xhr = new XMLHttpRequest();
          xhr.withCredentials = false;
          xhr.open('POST', '?s=admin&m=upload&c=image');
          xhr.onload = function () {
            var json;
            if (xhr.status != 200) {
              failFun('HTTP Error: ' + xhr.status);
              return;
            }
            json = JSON.parse(xhr.responseText);
            if (!json || typeof json.location != 'string') {
              failFun('Invalid JSON: ' + xhr.responseText);
              return;
            }
            succFun(json.location);
          };
          formData = new FormData();
          formData.append('file', file, file.name);
          xhr.send(formData);
        },
        file_picker_callback: function (callback, value, meta) {
TINYMCE;
    $arr_tinymce_ext = [",", ", ", " , "];
    echo '//文件分类
          var filetype=".' . str_replace($arr_tinymce_ext, ",.", FILE_EXT) . '";
          //后端接收上传文件的地址
          var upurl="?s=admin&m=upload&c=file";
          //为不同插件指定文件类型及后端地址
          switch(meta.filetype){
            case "image":
              filetype=".' . str_replace($arr_tinymce_ext, ",.", IMG_EXT) . '";
              upurl="?s=admin&m=upload&c=image";
              break;
            case "media":
              filetype=".' . str_replace($arr_tinymce_ext, ",.", MEDIA_EXT) . '";
              upurl="?s=admin&m=upload&c=media";
              break;
            case "file":
            default:
          }';
    echo <<<TINYMCE
          //模拟出一个input用于添加本地文件
          var input = document.createElement('input');
          input.setAttribute('type', 'file');
          input.setAttribute('accept', filetype);
          input.click();
          input.onchange = function() {
            var file = this.files[0];

            var xhr, formData;
            console.log(file.name);
            xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST', upurl);
            xhr.onload = function() {
              var json;
              if (xhr.status != 200) {
                  failure('HTTP Error: ' + xhr.status);
                  return;
              }
              json = JSON.parse(xhr.responseText);
              if (!json || typeof json.location != 'string') {
                  failure('Invalid JSON: ' + xhr.responseText);
                  return;
              }
              callback(json.location);
            };
            formData = new FormData();
            formData.append('file', file, file.name );
            xhr.send(formData);
          };
        }
      }).then(function (res) {
        // console.log(res)
      });
    </script>
TINYMCE;
  }
}